package wangxiaotao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import wangxiaotao.entity.User;
import wangxiaotao.entity.UserRoleRel;
import wangxiaotao.mapper.UserMapper;
import wangxiaotao.mapper.UserRoleRelMapper;
import wangxiaotao.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import wangxiaotao.tools.ConstValue;
import wangxiaotao.bean.vo.UserVO;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-03-22
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    private final ConstValue constValue;
    private final UserRoleRelMapper userRoleRelMapper;

    /**
     * 管理员权限下新增用户
     * @param userVO
     * @return
     */
    @Override
    public Integer userAdd(UserVO userVO) {
        User user = new User();
        BeanUtil.copyProperties(userVO, user);
        user.setPassword(constValue.getDefaultPassword());//默认密码
        user.setAvatar(constValue.getAvatar());//默认头像
        user.setStatue(constValue.getDefaultStatue());//默认状态
        //存储用户
        this.save(user);
        //插入默认角色
        return userRoleRelMapper.insert(new UserRoleRel().setUserId(user.getId()).setRoleId(constValue.getDefaultRoleId()));
    }
}
